/**
 * 收银中心管理初始化
 */
var TOrderFee = {
    id: "TDITable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    lock: false
};

/**
 * 初始化表格的列
 */
TOrderFee.initColumn = function () {
    return [

        {title: '单号', field: 'original_num', visible: true, align: 'center', valign: 'middle'},
        {title: '收货人', field: 'consignee', visible: true, align: 'center', valign: 'middle'},
        {title: '货物名称', field: 'goods_name', visible: true, align: 'center', valign: 'middle'},
        {title: '件数', field: 'goods_piece', visible: true, align: 'center', valign: 'middle'},
        {title: '提付', field: 'payment', visible: true, align: 'center', valign: 'middle'},
        {title: '代收货款', field: 'collection', visible: true, align: 'center', valign: 'middle'},
        {title: '落地费', field: 'extra_money', visible: true, align: 'center', valign: 'middle'},
        {title: '送货费', field: 'shf', event: 'a', visible: true, align: 'center', valign: 'middle'},
        {title: '搬运费', field: 'byf', event: 'b', visible: true, align: 'center', valign: 'middle'},
        {title: '仓储费', field: 'ccf', event: 'c', visible: true, align: 'center', valign: 'middle'},
        {title: '合计费用', field: 'suma', visible: true, align: 'center', valign: 'middle'},
        {title: '车费', field: 'cf', event: 'd', visible: true, align: 'center', valign: 'middle'},
        {title: '通知发货', field: 'notice', visible: true, align: 'center', valign: 'middle'},
        {title: '回单', field: 'receipt', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 关闭此对话框
 */
TOrderFee.close = function () {
    parent.layer.close(window.parent.table1.layerIndex);
}
/**
 * 提交修改
 */
TOrderFee.editSubmit = function () {


    if ($('#driverId').val() == '') {
        Feng.error("配送员不能为空!");
        return;
    }

    var w = window.open();
    if(!TOrderFee.lock) {
        //防止多次点击
        TOrderFee.lock = true; // 锁定
        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/tOrder/addAndTrian", function (data) {
            Feng.success("操作成功!");
            window.parent.table1.table.refresh();
            window.parent.ids.splice(0, window.parent.ids.length);
            window.parent.removeAll();
            TOrderFee.close();
            w.location = Feng.ctxPath + "/tOrder/tTrainDisPrint/" + data.code
        }, function (data) {
            Feng.error("操作失败!" + data.responseJSON.message + "!");
        });
        ajax.set("driver_name");
        ajax.set("driverId");
        ajax.set("ids");
        ajax.set("phone");
        ajax.set("cf");
        ajax.set("xj", $('#xj').html())
        ajax.set("car_no", $('#carNo').val());
        ajax.set("temOrderFeeS", JSON.stringify(vDate));
        ajax.start();
    }
}


Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};
var temOrderFeeS = new Array();

function mUpdata(obj, type, field) {
    var data = obj.data;
    var mTitle;
    var mValue = 0;
    if (type == 'a') {
        mValue = data.shf;
        mTitle = "送货费";
    } else if (type == 'b') {
        mValue = data.byf;
        mTitle = "搬用费";
    } else if (type == 'c') {
        mValue = data.ccf;
        mTitle = "仓储费";
    } else if (type == 'd') {
        mValue = data.cf;
        mTitle = "车费";
    }
    //这里一般是发送修改的Ajax请求

    //同步更新表格和缓存对应的值
    layer.prompt({
        formType: 2
        , title: '修改' + mTitle + '金额'
        , value: mValue
    }, function (value, index) {
        value = value.trim();

        var reg = /^\-?[0-9]+\.?[0-9]*$/;
        if (!reg.test(value)) {
            Feng.error("输入数字不规范，请重新输入");
            return false;
        }
        var mSum = 0;
        var fee_id = "";
        if (type == 'a') {
            mSum = (parseFloat(data.payment) + parseFloat(data.collection) + (parseFloat(data.byf) + parseFloat(data.ccf) + parseFloat(value) + parseFloat(data.extra_money)));
            fee_id = "27";
            obj.update({
                suma: mSum,
                shf: value
            });
            obj.data.shf = value;
            obj.data.mSum = mSum;

        } else if (type == 'b') {
            mSum = (parseFloat(data.payment) + parseFloat(data.collection) + (parseFloat(data.shf) + parseFloat(data.ccf) + parseFloat(value) + parseFloat(data.extra_money)));
            obj.update({
                suma: mSum,
                byf: value
            });
            obj.data.byf = value;
            obj.data.mSum = mSum;
            fee_id = "28";
        } else if (type == 'c') {
            mSum = (parseFloat(data.payment) + parseFloat(data.collection) + (parseFloat(data.shf) + parseFloat(data.byf) + parseFloat(value) + parseFloat(data.extra_money)));
            fee_id = "29";
            obj.update({
                suma: mSum,
                ccf: value
            });
            obj.data.ccf = value;
            obj.data.mSum = mSum;
        } else if (type == 'd') {
            mSum = (parseFloat(data.payment) + parseFloat(data.collection) + (parseFloat(data.shf) + parseFloat(data.byf) + parseFloat(data.ccf) + parseFloat(data.extra_money)));
            fee_id = "30";
            obj.update({
                suma: mSum,
                cf: value
            });
            obj.data.cf = value;
            obj.data.mSum = mSum;
        }
        upMdate(obj.data, mSum);
        layer.close(index);
    });
}

function upMdate(oData, oSum) {
    var mSum = 0;
    var uSum = 0;
    for (var i = 0; i < this.vDate.length; i++) {
        if (this.vDate[i].id == oData.id) {
            oData.suma = oSum;
            this.vDate[i] = oData;
            mSum = mSum + (parseFloat(oData.suma));
            uSum = uSum + (parseFloat(oData.cf));
        } else {
            mSum = mSum + (parseFloat(this.vDate[i].suma));
            uSum = uSum + (parseFloat(this.vDate[i].cf));
        }
    }
    $('#xj').text(mSum - uSum);
    $('#cf').val(uSum);
}

function cfSum(mVal) {
    mVal = mVal.trim();
    if (!isNaN(mVal)) {
        var a = Math.floor(eval(mVal).div(eval(this.vDate.length)));
        var b = eval(mVal).sub(eval(a).mul(this.vDate.length - 1));
        for (var i = 0; i < this.vDate.length; i++) {
            if (i == this.vDate.length - 1) {
                this.vDate[i].cf = b;
            } else {
                this.vDate[i].cf = a;
            }
            this.vDate[i].suma = parseFloat(this.vDate[i].payment).add(parseFloat(this.vDate[i].collection)).add(parseFloat(this.vDate[i].shf)).add(parseFloat(this.vDate[i].byf)).add(parseFloat(this.vDate[i].extra_money)).add(parseFloat(this.vDate[i].ccf));
        }
        mTable.reload(TOrderFee.id, {
            url: '',
            data: vDate
        })

    }
}

var vDate = null;

function collectSum(mDate) {
    this.vDate = mDate.data;
    var uSum = 0;
    var mSum = 0;
    for (var i = 0; i < mDate.data.length; i++) {
        uSum = uSum.add(mDate.data[i].cf);
        mSum = mSum.add(parseFloat(mDate.data[i].suma));
    }
    $('#cf').val(uSum);
    $('#xj').text(mSum - uSum);

}

var aTable = null
var mTable = null;
$(function () {
    var defaultColunms = TOrderFee.initColumn();
    aTable = new LYTable(TOrderFee.id, "/tOrder/mDataList?ids=" + $('#ids').val(), defaultColunms);
    layui.use('table', function () {
        mTable = layui.table;
        TOrderFee.table = aTable.initCallBack(mTable, collectSum);
        mTable.on('tool(' + TOrderFee.id + ')', function (obj) {
            mUpdata(obj, obj.event, obj.field);
        })
    })

    var a;
    $("#driver_name").bsSuggest('init', {
        /*url: "/rest/sys/getuserlist?keyword=",
        searchFields: [ "shortAccount"],*/
        effectiveFieldsAlias:{driver_name: "司机"},
        effectiveFields: ["id", "driver_name"],
        clearable: true,
        url: Feng.ctxPath + "/tOrder/searchList",
        idField: "id",
        keyField: "driver_name,driver_card"
    }).on('onDataRequestSuccess', function (e, result) {
        console.log('onDataRequestSuccess: ', result);
        a = result.value;
    }).on('onSetSelectValue', function (e, keyword, data) {
        // console.log('onSetSelectValue: ', keyword, e);
        // for (var i = 0; i < a.length; i++) {
        //     console.log('onSetSelectValue: ', a[i].id);
        //     if (a[i].id == keyword.id) {
        //         $('#phone').val(a[i].phone);
        //         $('#carNo').val(a[i].car_no);
                $('#driverId').val(keyword.id);
        //
        //     }
        // }
    }).on('onUnsetSelectValue', function () {
        console.log('onUnsetSelectValue');
        $('#driverId').val('');
    });

});